/*
	   _           _      _           _     _ 
	  (_)         | |    | |         (_)   | |
	   _ _   _ ___| |_ __| |_ __ ___  _  __| |
	  | | | | / __| __/ _` | '__/ _ \| |/ _` |
	  | | |_| \__ \ || (_| | | | (_) | | (_| |
	  | |\__,_|___/\__\__,_|_|  \___/|_|\__,_|
	 _/ |                                     
	|__/ 
	
	Copyright 2013 Caspar Isemer and and Eva Krueger, http://justdroid.org
	
	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at
	
		http://www.apache.org/licenses/LICENSE-2.0
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License. 
*/

package org.justdroid.justdroid;

import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class SettingsVolume extends JustDroidActivity {

	/** Declare variables and UI elements **/
	private RadioGroup rgVolume;
	
	/** Function to attach onTouchListener and onFocusListener to the text views **/
	void attachListenerToTextView(TextView textView) {
		/*textView.setOnTouchListener(new OnTouchListener() {	
			@Override
			public boolean onTouch(View view, MotionEvent arg1) {
				giveFeedback(((TextView)view).getText().toString());
				return false;
			}
		});*/
		
		textView.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View view, boolean hasFocus) {
				if(hasFocus) {
					giveFeedback(((TextView)view).getText().toString());
				}
			}
		});
	}

	/** Function to announce the text of the view that received focus or on which the onTouch event was triggered**/
	void giveFeedback(String text) {
		if(text.trim().equals(getResources().getString(R.string.minus)))
			text = getResources().getString(R.string.minusDescription);
		//vibrate
		Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
	    vibrator.vibrate(300);
	    //TTS feedback
	    if(!TTS.isSpeaking())
	    	TTS.speak(text);
	}
	
	/** Create the Contact Details menu activity **/
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.settingsvolume);
		super.onCreate(savedInstanceState);
		
		Utils.audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
		
		/** get UI elements **/
		rgVolume = (RadioGroup) findViewById(R.id.rgVolume);
		
		attachListener((RadioButton) findViewById(R.id.radioLoud));
		attachListener((RadioButton) findViewById(R.id.radioLouder));
		attachListener((RadioButton) findViewById(R.id.radioLoudest));
		attachListener((RadioButton) findViewById(R.id.radioNormal));
		attachListener((RadioButton) findViewById(R.id.radioSoft));
		attachListener((RadioButton) findViewById(R.id.radioSofter));
		attachListener((RadioButton) findViewById(R.id.radioSoftest));

		rgVolume.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup rg, int radioButtonId) {
				switch(radioButtonId) {
				case R.id.radioSoftest:
					Utils.audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 3, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
					giveFeedback(((RadioButton)findViewById(radioButtonId)).getText().toString());
					break;
				case R.id.radioSofter:
					Utils.audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 5, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
					giveFeedback(((RadioButton)findViewById(radioButtonId)).getText().toString());
					break;
				case R.id.radioSoft:
					Utils.audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 7, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
					giveFeedback(((RadioButton)findViewById(radioButtonId)).getText().toString());
					break;
				case R.id.radioNormal:
					Utils.audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 9, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
					giveFeedback(((RadioButton)findViewById(radioButtonId)).getText().toString());
					break;
				case R.id.radioLoud:
					Utils.audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 11, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
					giveFeedback(((RadioButton)findViewById(radioButtonId)).getText().toString());
					break;
				case R.id.radioLouder:
					Utils.audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 13, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
					giveFeedback(((RadioButton)findViewById(radioButtonId)).getText().toString());
					break;
				case R.id.radioLoudest:
					Utils.audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, Utils.audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
					giveFeedback(((RadioButton)findViewById(radioButtonId)).getText().toString());
					break;
				}
			}
		});
		
		/** Put most everything before here **/
	}
	
	@Override
	public void onResume() {
		super.onResume();
		if(Utils.audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) <= 3) {
			((RadioButton)findViewById(R.id.radioSoftest)).setChecked(true);
		}
		else if(Utils.audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) <= 5) {
			((RadioButton)findViewById(R.id.radioSofter)).setChecked(true);
		}
		else if(Utils.audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) <= 7) {
			((RadioButton)findViewById(R.id.radioSoft)).setChecked(true);
		}
		else if(Utils.audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) <= 9) {
			((RadioButton)findViewById(R.id.radioNormal)).setChecked(true);
		}
		else if(Utils.audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) <= 11) {
			((RadioButton)findViewById(R.id.radioLoud)).setChecked(true);
		}
		else if(Utils.audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) <= 13) {
			((RadioButton)findViewById(R.id.radioLouder)).setChecked(true);
		}
		else if(Utils.audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) <= Utils.audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)) {
			((RadioButton)findViewById(R.id.radioLoudest)).setChecked(true);
		}
		//get the root layout
		LinearLayout layout = (LinearLayout) findViewById(R.id.settingsfont);
		Utils.applyFontColorChanges(getApplicationContext(), layout);
		Utils.applyFontSizeChanges(getApplicationContext(), layout);  
		Utils.applyFontTypeChanges(getApplicationContext(), layout);
	}
}
